/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.rentalofconstructionmachinery.service.impl;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dao.CustomerDAO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.CustomerDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.converters.CustomerDTOConverter;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Customer;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.CustomerService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author stefan.rakonjac
 */
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
    
    @Autowired
    private CustomerDAO customerDAO;
    
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Override
    @Transactional
    public void addCustomer(CustomerDTO customer) {
        if (null == customer) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        customerDAO.createCustomer(CustomerDTOConverter.dtoToEntity(customer));
    }

    @Override
    @Transactional
    public void removeCustomer(CustomerDTO customer) {
        if (null == customer) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        customerDAO.deleteCustomer(CustomerDTOConverter.dtoToEntity(customer));
    }

    @Override
    @Transactional
    public void modifyCustomer(CustomerDTO customer) {
        if (null == customer) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        customerDAO.updateCustomer(CustomerDTOConverter.dtoToEntity(customer));
    }

    @Override
    @Transactional
    public List<CustomerDTO> getAllCustomers() {
        List<Customer> list = customerDAO.getAllCustomers();
        return CustomerDTOConverter.entityToDTOList(list);
    }

    @Override
    @Transactional
    public CustomerDTO getCustomerById(Long id) {
        if (null == id) {
            throw new IllegalArgumentException("'id' must not be null!");
        }
        return CustomerDTOConverter.entityToDTO(customerDAO.getCustomerById(id));
    }
}
